//
//  PersonalInformationViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/11.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "PersonalInformationViewController.h"
#import "PersonalInformationTableViewCell.h"

#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

#import "EditNameViewController.h"
#import "AmendBirthdayViewController.h"
#import "PersonalizedSignatureViewController.h"

static NSString *const PensonalInformationTableViewCell_ID = @"PensonalInformationTableViewCell_ID";

@interface PersonalInformationViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>



@property (nonatomic, strong) UITableView *tableView;//tableView

@property (nonatomic, strong) NSArray *titleArr;

@end

@implementation PersonalInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人信息";
    _titleArr = @[@"头像",@"姓名",@"生日",@"个性签名"];
    [self setupConstraints];
    
}
- (void)setupConstraints {
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_tableView reloadData];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[PersonalInformationTableViewCell class] forCellReuseIdentifier:PensonalInformationTableViewCell_ID];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _tableView;
}

#pragma mark -
#pragma mark tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonalInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PensonalInformationTableViewCell_ID forIndexPath:indexPath];
    
    cell.titleLbl.text = _titleArr[indexPath.section];
    [cell setType:ISInformationType];

    UserInfoModel *model = (UserInfoModel *)[UserManager sharedInstance].userModel.value;
    
    if (indexPath.section == 0) {
        [cell setType:ISHeaderType];
    } else if (indexPath.section == 1) {
        cell.informationLbl.text = model.displayName;
    } else if (indexPath.section == 2) {
        cell.informationLbl.text = model.birthday;
    } else if (indexPath.section == 3) {
        cell.informationLbl.text = model.signature;
    }

    return cell;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 80;
    } else {
        return 50;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.0001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.0001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:
            {
//                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//                [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                    UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
//                    PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//                    PickerImage.allowsEditing = YES;
//                    PickerImage.delegate = self;
//                    [self presentViewController:PickerImage animated:YES completion:nil];
//                }]];
//                [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
//                    UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
//                    PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
//                    PickerImage.allowsEditing = YES;
//                    PickerImage.delegate = self;
//                    [self presentViewController:PickerImage animated:YES completion:nil];
//                }]];
//                [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
//                [self presentViewController:alert animated:YES completion:nil];
            }
            break;
        case 1:
        {
            EditNameViewController *editname = [[EditNameViewController alloc] init];
            [self.navigationController pushViewController:editname animated:YES];
        }
            break;
        case 2:
        {
            AmendBirthdayViewController *amendBVC = [[AmendBirthdayViewController alloc] init];
            [self.navigationController pushViewController:amendBVC animated:YES];
        }
            break;
        case 3:
        {
            PersonalizedSignatureViewController *amendPVC = [[PersonalizedSignatureViewController alloc] init];
            [self.navigationController pushViewController:amendPVC animated:YES];
        }
            break;
            
        default:
            break;
    }
    
    
}
//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //定义一个newPhoto，用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
